package service;

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import mapper.RecordMapper;
import mapper.view.MRecordViewMapper;
import mapper.view.RRecordViewMapper;
import org.apache.ibatis.session.SqlSession;
import pojo.Record;
import pojo.view.MRecord;
import pojo.view.RRecord;
import util.DBUtil;

import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

public class RecordService {
    /**
     * 查询读者视图下的某条预约记录
     * @param floorName 楼层名
     * @param number 座位号
     * @param reserveTime 预约时间
     * @return RRecord
     */
    public RRecord queryBySeatAndTimeForReader(String floorName, int number, Timestamp reserveTime) {
        SqlSession sqlSession = null;
        RRecord record = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RRecordViewMapper mapper = sqlSession.getMapper(RRecordViewMapper.class);
            record = mapper.query(floorName, number, reserveTime);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return record;
    }

    /**
     * 查询管理员视图下的某条预约记录
     * @param floorName 楼层名
     * @param number 座位号
     * @param reserveTime 预约时间
     * @return MRecord
     */
    public MRecord queryBySeatAndTimeForManager(String floorName, int number, Timestamp reserveTime) {
        SqlSession sqlSession = null;
        MRecord record = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            MRecordViewMapper mapper = sqlSession.getMapper(MRecordViewMapper.class);
            record = mapper.query(floorName, number, reserveTime);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return record;
    }

    /**
     * 查询读者视图下的所有预约记录
     * @return List
     */
    public List<RRecord> queryAllRecordsForReader() {
        SqlSession sqlSession = null;
        List<RRecord> records = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RRecordViewMapper mapper = sqlSession.getMapper(RRecordViewMapper.class);
            records = mapper.queryAll();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return records;
    }

    /**
     * 查询管理员视图下的所有预约记录
     * @return List
     */
    public List<MRecord> queryAllRecordsForManager() {
        SqlSession sqlSession = null;
        List<MRecord> records = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            MRecordViewMapper mapper = sqlSession.getMapper(MRecordViewMapper.class);
            records = mapper.queryAll();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return records;
    }

    /**
     * 获取用户视图下用户未使用的座位记录
     * @param id 用户id
     * @return Record
     */
    public RRecord getUnUsedById(String id) {
        SqlSession sqlSession = null;
        RRecord record = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RRecordViewMapper mapper = sqlSession.getMapper(RRecordViewMapper.class);
            record = mapper.getUnUsedById(id);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return record;
    }

    /**
     * 获取用户视图下用户正在使用的座位记录
     * @param id 用户id
     * @return Record
     */
    public RRecord getInUseById(String id) {
        SqlSession sqlSession = null;
        RRecord record = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RRecordViewMapper mapper = sqlSession.getMapper(RRecordViewMapper.class);
            record = mapper.getInUseById(id);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return record;
    }

    /**
     * 获取用户视图下用户未使用的座位记录
     * @return Record
     */
    public List<MRecord> getUnUsed() {
        SqlSession sqlSession = null;
        List<MRecord> records = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            MRecordViewMapper mapper = sqlSession.getMapper(MRecordViewMapper.class);
            records = mapper.getUnUsed();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return records;
    }

    /**
     * 获取管理员视图下所有正在使用的座位记录
     * @return Record
     */
    public List<MRecord> getInUse() {
        SqlSession sqlSession = null;
        List<MRecord> records = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            MRecordViewMapper mapper = sqlSession.getMapper(MRecordViewMapper.class);
            records = mapper.getInUse();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return records;
    }

    /**
     * 获取用户视图下已完成的座位记录
     * @param id 用户id
     * @return List<Record>
     */
    public List<RRecord> getCompletedById(String id) {
        SqlSession sqlSession = null;
        List<RRecord> records = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            RRecordViewMapper mapper = sqlSession.getMapper(RRecordViewMapper.class);
            records = mapper.getCompletedById(id);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return records;
    }

    /**
     * 获取管理员视图下所有已完成的座位记录
     * @return List<Record>
     */
    public List<MRecord> getCompleted() {
        SqlSession sqlSession = null;
        List<MRecord> records = null;
        try {
            sqlSession = DBUtil.getSqlSession();
            MRecordViewMapper mapper = sqlSession.getMapper(MRecordViewMapper.class);
            records = mapper.getCompleted();
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (sqlSession != null)
                sqlSession.close();
        }
        return records;
    }
}
